<?php

namespace App\Http\Controllers\Home;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;

class AuthControllers extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    //登录
    public function login()
    {

        return view('home/index/login');
    }

    //返回注册界面
    public function register()
    {
        return view('home/index/register');
    }

    //注册
    public function userRegister(Request $request)
    {
        $rules = ['captcha' => 'required|captcha'];
        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()){
            return redirect('register')->with('errCaptcha','验证码错误');
        }else{
            $uname = $request->input('username');
            $birth = $request->input('birth');
            $email = $request->input('email');
            $pwd = Hash::make($request->input('password'));
            if(!empty($uname)&&!empty($birth)&&!empty($email)&&!empty($pwd)){
                $register = new User;
                $register->email = $email;
                $register->password = $pwd;
                $register->uname = $uname;
                $register->birth = $birth;
                $register->save();
                return redirect('login');
            }else{
                return redirect('')->with('message','注册失败');
            }
        }


    }

    //用户名查重
    public function auth(Request $request)
    {
        $info = new User;
        if (!empty($request->input('content'))) {
            $name = $request->input('content');
            $uname = $info->where('uname', $name)->get();
            if (count($uname) == 0) {
                return response()->json(array(
                    'message' => ''
                ));
            } else {
                return response()->json(array(
                    'message' => '用户名已存在'
                ));
            }
        }
        if (!empty($request->input('email'))) {
            $email = $request->input('email');
            $sqlEmail = $info->where('email', $email)->get();
            if (count($sqlEmail) == 0) {
                return response()->json(array(
                    'message' => ''
                ));
            } else {
                return response()->json(array(
                    'message' => '邮箱已存在'
                ));
            }
        }
    }

    //验证码验证
    public function code()
    {
        $rules = ['captcha' => 'required|captcha'];
        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()){
            return response()->json(array(
                'message' => '验证码错误'
            ));
        }else{
            return response()->json(array(
                'message' => null
            ));
        }
    }

}
